package net.kesumu.yushabu;

import com.umeng.message.PushAgent;

import net.kesumu.general.android.EasyStorage.EasyStorage;
import net.kesumu.general.android.view.CustomStatusBar;
import net.kesumu.yushabu.R;
import net.kesumu.yushabu.R.id;
import net.kesumu.yushabu.R.layout;
import net.kesumu.yushabu.R.menu;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.TextView.OnEditorActionListener;
import android.os.Build;

public class FriendAddActivity extends Activity {

    private EasyStorage mEasyStorage;
    
    String mMyId;
    
    TextView mTextMessageView;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_friend_add);
        getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
        getActionBar().setCustomView(R.layout.actionbar);
        ((TextView)findViewById(R.id.actionbar_title)).setText(getString(R.string.title_activity_friend_add));
        //Custom the status bar
        CustomStatusBar customStatusBar = new CustomStatusBar(this);
        customStatusBar.setStatusBarColor(findViewById(R.id.statusBarBackground), getResources().getColor(R.color.main));

        
        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }
        
        mEasyStorage = new EasyStorage(this, getString(R.string.preference_file_key));
        mMyId = mEasyStorage.getString("mMyId");
        
        /*Intent receiveIntent = getIntent();
        mMyId = receiveIntent.getStringExtra("myId");*/
        
        PushAgent.getInstance(this).onAppStart();
    }

    @Override
    protected void onStart() {
        mTextMessageView = (TextView) findViewById(R.id.friendKeyWord);
        mTextMessageView.requestFocus(MODE_APPEND);
        mTextMessageView.setImeActionLabel(getResources().getString(R.string.btn_search), 
                                                            KeyEvent.KEYCODE_ENTER);
        mTextMessageView.setOnEditorActionListener(new OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                boolean handled = false;
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    searchFriend(findViewById(R.id.btn_searchFriend));
                    handled = true;
                }
                return handled;
            }
        });
        super.onStart();
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.friend_add, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
//        if (id == R.id.action_settings) {
//            return true;
//        }
        return super.onOptionsItemSelected(item);
    }
    
    public void searchFriend(View clickedView) {
        TextView mFriendKeyWordView = (TextView) findViewById(R.id.friendKeyWord);
        String mFriendKeyWord = mFriendKeyWordView.getText().toString();
        if(mFriendKeyWord == null || mFriendKeyWord.equalsIgnoreCase("") || mFriendKeyWord.equalsIgnoreCase(getString(R.string.prompt_friendKeyWord))) {
            showToast("Should not be blank");
            return;
        }
        Intent friendSearchAcitivityIntent = new Intent(FriendAddActivity.this,FriendSearchActivity.class);
        friendSearchAcitivityIntent.putExtra("friendKeyWord", mFriendKeyWord);
//        friendSearchAcitivityIntent.putExtra("myId", mMyId);
        startActivity(friendSearchAcitivityIntent);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_friend_add,
                    container, false);
            return rootView;
        }
    }
    
    private void showToast(final String message) {
        runOnUiThread(new Runnable() {
            public void run() {
                Toast.makeText(getApplicationContext(), message,
                        Toast.LENGTH_SHORT).show();
            }
        });
 
    }

}
